package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.contant.StatusContant;
import com.qk.domain.clue.ClueDO;
import com.qk.dto.clue.*;
import com.qk.entity.Clue;
import com.qk.management.aop.annotation.LogAnno;
import com.qk.management.service.ClueService;
import com.qk.vo.clue.CluePoolLitsVO;
import com.qk.vo.clue.ClueVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/clues")
@Slf4j
public class ClueController {
    @Autowired
    private ClueService clueService;
    /** 线索池列表查询 */
    @GetMapping("/pool")
    public Result cluePoolSelectPage(CluePoolListDTO dto){
        PageResult<CluePoolLitsVO> pageResult=clueService.cluePoolSelectPage(dto);
        return Result.success(pageResult);
    }



    /** 线索跟进记录 */
    @LogAnno
    @PutMapping
    public Result clueFollow(@RequestBody UpdateClueInfoDTO dto){
        clueService.updateClueInfoById(dto);
        return Result.success();
    }

    /** 列表查询 */
    @GetMapping
    public Result list(ClueListDTO dto) {
        PageResult<ClueDO> pageResult = clueService.clueList(dto);
        return Result.success(pageResult);
    }

    /** 添加线索 */
    @LogAnno
    @PostMapping
    public Result addClue(@RequestBody ClueDTO clueDTO){
        clueService.addClue(clueDTO);
        return Result.success();
    }

    /** 分配线索 */
    @LogAnno
    @PutMapping("/assign/{clueId}/{userId}")
    public Result assignClueUser(@PathVariable Integer clueId,@PathVariable Integer userId){
        clueService.updateById(Clue.builder()
                        .id(clueId)
                        .userId(userId)
                        .updateTime(LocalDateTime.now())
                        .status(StatusContant.WAIT_FOLLOW_UP)
                        .build());
        return Result.success();
    }

    /** 伪线索 */
    @LogAnno
    @PutMapping("/false/{id}")
    public Result fasleClue(
            @PathVariable(required = true) Integer id,
            @RequestBody ClueReasonDTO clueReasonDTO
            ){
        clueService.falseClue(id,clueReasonDTO);

        return Result.success();
    }

    /** 转商机 */
    @LogAnno
    @PutMapping("/toBusiness/{id}")//该接口用于将指定线索转为商机
    public Result toBusiness(@PathVariable Integer id){
        clueService.toBusiness(id);
        return Result.success();
    }

    /** 根据ID查询 */
    @GetMapping("/{id}")
    public Result getClue(@PathVariable Integer id){
        ClueVO clueVO = clueService.getClueById(id);
        return Result.success(clueVO);
    }


    /*@PutMapping("/{clueId}/{status}")
    public Result updateClueStatus(
            @PathVariable(required = true) Integer clueId,
            @PathVariable Integer reason,
            @PathVariable String remark
    ) {
        clueService.updateClueStatus(clueId, status);
        return Result.success();
    }*/
}
